Okay, so I actually lied.
There is one more thing I would like to add to our game, and that is adding a feature where we can
save the tools of a player when they die, and give those tools back to them when they respawn in the
game.
This can alleviate the frustration of players losing their tools when they die and joining back into
the game with no tools.
Imagine if there were players on wave ten, most of them dying, and then when they spawned back into
wave 11, they had no guns to defend themselves.
Thankfully, this kind of system will be easy to implement, so let's go ahead and create a new server
script inside of server Script service.
And I'm going to call this my backpack save handler.
We'll paste in that comment template, get rid of that section and get rid of that section.
And then we'll go ahead and get the player service.
And what we want to do is we want to listen for when a player is added into our game.
And we're going to create a new folder inside of this player instance, and we can call it something
like Backpack Save.
So that way when our player dies, we can go ahead and move all their tools into that folder.
And when the player respawns back on the live team, then we can go ahead and move those tools back
into the player's backpack.
So we'll create a new folder I'm going to call it Backpack Save.
That's equal to instance dot new folder.
The name is going to be equal to backpack save.
And then we'll set the parent equal to this player.
And then here is where we can go ahead and listen for when a character A is added to our player, get
that character.
And then we'll create a dedicated function to listen for when a character is added to a player.
So we'll call it on character added.
And we want to get past the player and the associated character.
And then we can just call on character added pass our player, pass our character.
And then what we can go ahead and do is when a player has a new character added to them, we want to
check if this player is on the alive team.
That way we can move any existing tools that are inside of their backpack safe folder into their backpack.
So let's go ahead and grab the team service, get service teams and if this player dot team is equal
to the alive team and they just had a character added to them, that means they respawned into the game.
So we need to go ahead and check to see if there's any tools inside of our backpack save folder.
So we're going to get all the children in there if there are any.
And what we'll do is we'll set the parent of that tool equal to the player's backpack.
The other event we want to go ahead and listen to for this character is when they die.
So let's go ahead and make another function specifically for listening for when a humanoid dies, we'll
call on humanoid death.
And let's go ahead and pass a player and a character to this function.
And then down here we'll get this character, get their humanoid, listen to the died event, and we
can connect a lambda function.
And then inside of here we'll call our on humanoid death function.
Pass this player and their character.
And now what we need to do is when this player dies, we need to verify that the game state is not in
the game over game state.
So what we can go ahead and do is get the game state which is equal to workspace, get attribute game
state.
And that means we're also going to have to grab the game state enum.
So let's go ahead and get replicated storage.
And then we can make a variable for the game state enum, and we'll require a replicated storage modules
dot enums dot game state enum.
And then what we can do is if this game state is equal to the game state enum of game over, or if this
game state is equal to the game state enum of game over when, well then clearly we don't want to save
the tools of this player, so we're just going to return.
Otherwise we can go ahead and do is we can loop through all of the tools in the player's backpack and
place them inside of the backpack save folder.
However, we only want to do this for their guns or their med kits or their grenades because when the
player spawns in the game, they're automatically going to be given any knife tools.
So we don't want to save any of the knife tools.
So we'll loop through every single tool in this player's backpack.
Get children.
And what we'll do is we'll check to see if this tool has the name of knife.
So we'll find if inside of that name it has the string of knife.
If it does then we don't want to save this tool.
We'll just continue to the next one.
Otherwise we can go ahead and set the parent of this tool equal to the player's backpack save folder.
Another thing we want to check is if the player had a tool equipped.
So we're going to check to see if their character find first child, which is a tool.
If they don't have a tool on their character, then we'll just return.
Otherwise, we also want to save this tool into the backpack of the player and only if it's not a knife.
So if string.find on this tool dot name, if it has the word knife in it, then we're just going to
return as well.
Otherwise we can go ahead and save this tool that they have equipped.
So tool dot parent is going to be equal to player dot backpack save.
Now.
Actually, a better idea I have is we should check here if the game state is actually equal to the game
state enum of waiting, which means we're cleaning up the game.
So that way we don't save any tools for the players that get killed during the waiting stage.
And then what we should do is we should listen for when the workspace gets set to this game.
State of waiting.
And we should loop through all the players in our game and clear out any of the tools that might be
inside of their backpack.
Save folders.
So let's go ahead and also listen for when the workspace get attribute change signal of game state changes.
We'll connect a function to this and get the current game state which is equal to workspace.
Get attribute game state.
And if the game state.
Is equal to the game state enum.
Of waiting.
Then we should loop through every single player in our game.
So, players.
Get players.
And what we should do is we should refer to this players.
Backpack save folder and then clear all children inside of that folder.
So now this is deleting any previous tools they had from a previous game.
And that way when a new game starts up, every player will be fresh and they won't have any previous
tools inside of their backpack save folder.
So now we can go ahead and test this out by creating a two player server here on the server.
I'm going to be giving my players some money, so for their money value I'll just give them a whole
bunch of money.
And then what we can go ahead and do is we can spawn in these players and let's go buy some stuff for
these players.
So I'll just buy all of the guns.
Boom, boom, boom, boom, boom boom, boom boom.
Okay, perfect.
And then we can do the same thing for this guy.
We'll buy all the stuff in the shop.
Boom, boom, boom, boom, boom boom boom boom.
Okay, cool.
So now what I want to do is I want to have one of these players die, which means when they die, all
of these tools should be placed inside of that folder.
And then I want to have this player win and clean up the game to allow this guy to spawn back in.
And when he spawns back in, he should have all of his tools left.
So let's go ahead and see what happens.
I'm going to lead these zombies over here to come kill this guy.
So I'm just going to go walk out of the way and they should kill him.
Yep.
All right.
They're beating up.
Hopefully they kill him.
I'm not here.
I'm just kind of chilling.
He's about to die any second.
Yep.
Now he's dead.
So let's go ahead and clean up all these guys.
Three zombies left.
Where are they at?
And then.
Boom!
All right, now we're in intermission.
So if our guy spawns back into the game just like that.
As you can see, we got all of our tools back, so we didn't lose anything.
And we're not forced to have to go buy new tools.
All of them were saved and stored inside of our backpack saves folder.
Now let's go ahead and have both of our players die.
That way the game is over.
We lost and then it should clean up the game and delete all of our previous tools we have.
So that way when we spawn in to a fresh new game, we shouldn't have these tools anymore.
Let's go ahead and wait for this intermission to end, and then we'll have both of these guys die to
our zombies.
We're actually, you know, we could just reset right now.
So I'll reset this guy and I'll reset this guy.
All right, game over.
Zombies win!
All right, let's go ahead and spawn in.
And just like that, we don't have our old tools anymore.
Very cool.
All right.
In the next lecture, I'm going to be playtesting through the game to make sure everything is functioning
properly.
Make sure you do the same.
Play test through the game, balance out anything to your preferences, and I'll see you in the next
lecture.